package com.bitejiuyeke.biteportalservice.homepage.service.impl;

import com.bitejiuyeke.biteadminapi.map.domain.dto.LocationReqDTO;
import com.bitejiuyeke.biteadminapi.map.domain.vo.RegionCityVo;
import com.bitejiuyeke.biteadminapi.map.feign.MapFeignClient;
import com.bitejiuyeke.bitecommoncore.utils.BeanCopyUtil;
import com.bitejiuyeke.bitecommondomain.domain.R;
import com.bitejiuyeke.bitecommondomain.domain.ResultCode;
import com.bitejiuyeke.bitecommondomain.exception.ServiceException;
import com.bitejiuyeke.biteportalservice.homepage.domain.dto.CityDescDTO;
import com.bitejiuyeke.biteportalservice.homepage.domain.dto.DicDataDTO;
import com.bitejiuyeke.biteportalservice.homepage.domain.dto.PullDataListReqDTO;
import com.bitejiuyeke.biteportalservice.homepage.domain.vo.CityDescVO;
import com.bitejiuyeke.biteportalservice.homepage.domain.vo.DictsVO;
import com.bitejiuyeke.biteportalservice.homepage.domain.vo.PullDataListVO;
import com.bitejiuyeke.biteportalservice.homepage.service.IDictionaryService;
import com.bitejiuyeke.biteportalservice.homepage.service.IHomePageService;
import com.bitejiuyeke.biteportalservice.homepage.service.IRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    private MapFeignClient mapFeignClient;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDictionaryService dictionaryService;

    @Override
    public CityDescVO getCityDesc(Double lat, Double lon) {
        //先校验参数
        if(lat == null || lon == null){
            throw new ServiceException("城市经纬度不能为空！", ResultCode.INVALID_PARA.getCode());
        }
        //发起远程调用
        LocationReqDTO locationReqDTO = new LocationReqDTO();
        locationReqDTO.setLat(lat);
        locationReqDTO.setLng(lon);
        R<RegionCityVo> result = mapFeignClient.locateCityByLocation(locationReqDTO);
        if (null == result ||
        result.getCode() != ResultCode.SUCCESS.getCode() ||
        null == result.getData()){
            throw new ServiceException("根据定位获取城市信息失败");
        }
        CityDescVO cityDescVO = new CityDescVO();
        BeanUtils.copyProperties(result.getData(),cityDescVO);
        //构造返回
        return cityDescVO;
    }

    @Override
    public PullDataListVO getPullData(PullDataListReqDTO reqDTO) {
        PullDataListVO result = new PullDataListVO();

        //查询子区域
        List<CityDescDTO> cityDescDTOList = regionService.regionChildren(reqDTO.getCityId());

        result.setRegionList(
                BeanCopyUtil.copyListProperties(cityDescDTOList, CityDescVO::new)
        );

        //查询字典数据列表
        Map<String, List<DicDataDTO>> dicDataMap = dictionaryService.batchFindDictionaryDataByTypes(reqDTO.getDirtTypes());
        Map<String, List<DictsVO>> dictsMap = new HashMap<>();
        for (Map.Entry<String, List<DicDataDTO>> entry:
             dicDataMap.entrySet()) {
            List<DictsVO> list = entry.getValue()
                     .stream()
                     .map(dicDataDTO -> {
                         DictsVO dictsVO = new DictsVO();
                         dictsVO.setId(dicDataDTO.getId());
                         dictsVO.setKey(dicDataDTO.getDataKey());
                         dictsVO.setName(dicDataDTO.getDataKey());
                         return dictsVO;
                     })
                     .collect(Collectors.toList());
            dictsMap.put(entry.getKey(), list);
        }
        result.setDictMap(dictsMap);
        return result;
    }
}
